const app = getApp()
import { requestApi } from '../../../api';
import { dateFormat } from '../../../utils/dateFormat';
Page({
    data: {
        history: [],
        hotKey: [],
        entrenceTime: dateFormat('yyyy-MM-dd hh:mm:ss:S')
    },
    onLoad(options) {
        this.getHotKeys();
    },
    onShow() {
        const self = this;

        wx.getStorage({
            key: 'searchHistory',
            success (res) {
                self.setData({
                    history: res.data
                })
            }
        })
    },
    getHotKeys() {
        app.request({
            url: requestApi.hotKey,
            login: false,
            data: {
            },
        }, (data) => {
            if (data.original.ret_code == 200) {
                this.setData({
                    hotKey: data.original.data.hot_search_keywords
                })
            }
        }, (error) => {
            wx.showModal({
                title: '提示',
                showCancel: false,
                content: error,
            })
        });
    },
    delHistory() {
        this.setData({
            history: []
        })
        wx.removeStorageSync('searchHistory');
    },
    goSearch(e) {
        const value = e.currentTarget.dataset.key;
        const type = e.currentTarget.dataset.type || '6';
        let arr = [], history = wx.getStorageSync('searchHistory') || [];
        const index = history.findIndex(item => item == value);
        const { entrenceTime} = this.data;
        const opt_time = dateFormat('yyyy-MM-dd hh:mm:ss:S');

        if (index >= 0) {
            history.splice(index, 1);
        }

        arr = ([].concat([value], history)).slice(0, 9);
        wx.setStorageSync("searchHistory", arr);

        wx.navigateTo({
            url: '/pages/categoryHall/categoryHall?keywords=' + value + '&cur_page_resource_id='+ value +'&isSearch='+ type +'&ref_page_id=OFSearchViewController&ref_opt_resource_type=5&ref_opt_protocol=/pages/search/searchResult/searchResult&opt_resource_id=' + value + '&opt_protocol=/pages/categoryHall/categoryHall&opt_time=' + opt_time + '&log_type=1&ref_write_sever_timestamp=' + entrenceTime
        });
    }
})